﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoundMatcher.bean_classes;
using System.Data;
using NAligner;
using NAligner.formats;

namespace SoundMatcher
{
    class SoundMatcher
    {

        public List<int> GetBestFit(double[] input)
        {
           
            StringBuilder userInputSequence = new StringBuilder();
           
            userInputSequence.Append("*");

            double reference1 = input[0];
            for (int i = 1; i < input.Length; i++)
            {
                if (input[i] >= reference1)
                {
                    userInputSequence.Append("U");

                }
                else
                {
                    userInputSequence.Append("D");
                }


            }

            string query = "SELECT  frequencies, id, title FROM song";
            DataTable songsWithFrequencyDiffs = DB.GetDataTable(query);
            DataTableReader tableReader = songsWithFrequencyDiffs.CreateDataReader();

            Dictionary<int, double> matchingSongsWithAccuracy = new Dictionary<int, double>();
            List<double> accuracyList = new List<double>();
            List<int> matchingSongs = new List<int>();

            while (tableReader.Read())
            {

                int id = Convert.ToInt32(tableReader["id"]);
                string title = Convert.ToString(tableReader["title"]);
                string frequnciesString = Convert.ToString(tableReader["frequencies"]);

                double[] frequnciesOfOriginal = frequnciesString.Split(',').Select(n => double.Parse(n)).ToArray();


                StringBuilder OriginalSongSequence = new StringBuilder();
                OriginalSongSequence.Append("*");

                double reference2 = frequnciesOfOriginal[0];
                for (int i = 1; i < frequnciesOfOriginal.Length; i++)
                {
                    if (frequnciesOfOriginal[i] >= reference2)
                    {
                        OriginalSongSequence.Append("U");

                    }
                    else
                    {

                        OriginalSongSequence.Append("D");
                    }


                }

                Sequence s1 = new Sequence();
                s1.AAList = OriginalSongSequence.ToString();
                Sequence s2 = new Sequence();
                
                s2.AAList = userInputSequence.ToString();
                float[,] scoreMatrix=new float[,]{{1,2},{2,3}};
                Matrix m = new Matrix("mm", scoreMatrix);

                Alignment alignment = SmithWatermanGotoh.Align(s1, s2, m, 0f, 0f);

                
                Console.WriteLine(new Pair().Format(alignment));
                
                Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>");

                double accuracy = alignment.Score;

                matchingSongsWithAccuracy.Add(id, accuracy);
                accuracyList.Add(accuracy);
                
            }

            return null;
        }


//////////*************    Previous implementation      ******//////////////////////

        
    //   public List<int> GetBestFit(double[] input){

    //       double[] segmentfreDiference = new double[input.Length - 1];

    //       for (int j = 0; j < (input.Length - 1); j++)//loop to calculate fre difference from segment frequency array
    //       {
    //           segmentfreDiference[j] = input[j + 1] - input[j];
    //       }



    //       string query = "SELECT  freq_difference, id, title FROM song";
    //     DataTable songsWithFrequencyDiffs=DB.GetDataTable(query);
    //     DataTableReader tableReader = songsWithFrequencyDiffs.CreateDataReader();


    //     Dictionary<int,double> matchingSongsWithAccuracy = new Dictionary<int,double>();
    //       List<double> accuracyList=new List<double>();
    //       List<int> matchingSongs = new List<int>();

    //    while(tableReader.Read()){

    //       int id= Convert.ToInt32(tableReader["id"]);
    //       string title = Convert.ToString(tableReader["title"]);
    //       string freDifferencesString = Convert.ToString(tableReader["freq_difference"]);

    //       int[] freDifferencesOfOriginal = freDifferencesString.Split(',').Select(n =>int.Parse(n)).ToArray();

           
               
    //                int range = 0;
    //                int noOfSimilaFre = 0;


    //                for (int y = 0; y < segmentfreDiference.Length;y++ )
    //                {

    //                    int f = Convert.ToInt32(segmentfreDiference[y]);

    //                    bool success=freDifferencesOfOriginal.Contains(f);
    //                    if (success)
    //                   {
                           
    //                       noOfSimilaFre++;

    //                   }
    //                   else
    //                   {
    //                      int k=-1*(range/2);

    //                       while (k <= (range/2))
    //                       {



    //                           if(k==0){
    //                               k++;
    //                               continue;
    //                           }
    //                           bool freExist = freDifferencesOfOriginal.Contains(f + k);

    //                           if (freExist)
    //                           {

    //                               noOfSimilaFre++;
    //                           }
    //                           k++;
                               
    //                       }


    //                   }

                       


    //                }


    //                double accuracy = Convert.ToDouble( noOfSimilaFre) /Convert.ToDouble (segmentfreDiference.Length);

                
    //        //if(accuracy>.5){

    //            //Song s = new Song();
    //            //s.id = id;
    //            //s.title = title;

    //            matchingSongsWithAccuracy.Add(id, accuracy);
    //            accuracyList.Add(accuracy);

                
    //       // }

            
    //        }



    //    accuracyList.Sort();
    //    accuracyList.Reverse();

    //    foreach (double accuracy in accuracyList)
    //    {
    //        foreach (KeyValuePair<int,double> pair in matchingSongsWithAccuracy)
    //        {
    //            if (pair.Value == accuracy)
    //            {
    //                if(!matchingSongs.Contains(pair.Key)){
    //                    matchingSongs.Add(pair.Key);

    //                }
                    
    //            }
    //        }
    //    }

    //       return matchingSongs;
    //    }

    }
}
